//
//  YbrDeviceDao.m
//  wifibox
//
//  Created by xinle on 16/1/25.
//  Copyright © 2016年 yinbaner. All rights reserved.
//

#import "YbrDeviceDao.h"
#import "AppDelegate.h"
@implementation YbrDeviceDao

+(BOOL)insert:(YbrDevice *)item
{
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        rs = [db executeQueryWithFormat:@"select count(deviceIP) from device where deviceIP=%@" ,item.DeviceIpAddr];
        while (rs.next) {
            int count = [rs intForColumn:@"count(deviceIP)"];
            if (count == 0) {
                // 1.查询数据
                rs = [db executeQueryWithFormat:@"insert into device (deviceName ,deviceIP,userName,deviceVersion) VALUES (%@,%@,%@,%@)", item.DeviceName,item.DeviceIpAddr,item.userName,item.DeviceFiremwareVersion];
                while (rs.next) {
                    isOk = YES;
                    break;
                }
                [rs close];
            }
        }
        
    }];
    return isOk;
}

+(BOOL)insertArr:(NSArray *)itemArr
{
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        for (int i = 0; i < itemArr.count; i++) {
            YbrDevice *device = itemArr[i];
            rs = [db executeQueryWithFormat:@"insert into device (deviceName ,deviceIP,userName,deviceVersion) VALUES (%@,%@,%@,%@)", device.DeviceName,device.DeviceIpAddr,device.userName,device.DeviceFiremwareVersion];
            while (rs.next) {
                isOk = YES;
                break;
            }
            [rs close];
        }
        
        
    }];
    return isOk;
}

+(BOOL)updateUserByitem:(YbrDevice *)item userName:(NSString *)userName
{
    __block BOOL isOk = false;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
//        YbrLog(@"%@,%@",item.userName,item.DeviceIpAddr);
        // 1.修改数据
        rs = [db executeQueryWithFormat:@"update device set userName=%@ where deviceIP=%@",userName,item.DeviceIpAddr];
        while (rs.next) {
            isOk = YES;
            break;
        }
        [rs close];
    }];
    return isOk;
}

+(BOOL)isUpgradeByitem:(YbrDevice *)item upgrade:(int)upgrade
{
    __block BOOL isOk = false;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
//        YbrLog(@"%@,%@",item.userName,item.DeviceIpAddr);
        // 1.修改数据
        rs = [db executeQueryWithFormat:@"update device set isNeedUpgrade=%d where deviceIP=%@",upgrade,item.DeviceIpAddr];
        while (rs.next) {
            isOk = YES;
            break;
        }
        [rs close];
    }];
    return isOk;
}


+(BOOL)deleteById:(int)devId
{
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdateWithFormat:@"delete from device where _id = %i",devId];
    }];
    return isOk;
}


+(NSArray *)getDevice
{
    __block NSArray *arr;
    
    // 获取最新版本号
//    NSString * verion               =  [[AppDelegate app].perfer getHardFireVersion];
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs                          =   [db executeQueryWithFormat:@"select * from device"];
        
        NSMutableArray *tempArr     =   [NSMutableArray array];
        while (rs.next) {
            YbrDevice *devItem      =   [[YbrDevice alloc] init];
            devItem._id             =   [rs intForColumn:@"_id"];
            devItem.DeviceName      =   [rs stringForColumn:@"deviceName"];
            devItem.DeviceIpAddr    =   [rs stringForColumn:@"deviceIP"];
            devItem.userName        =   [rs stringForColumn:@"userName"];
            devItem.DeviceFiremwareVersion = [rs stringForColumn:@"deviceVersion"];
            devItem.isNeedUpgrade   =   [rs intForColumn:@"isNeedUpgrade"];

            // 判断固件版本需不需要更新
//            if ([devItem.DeviceFiremwareVersion isEqualToString:verion]) {
//                devItem.isNeedUpgrade   =   NO;
//            }
//            else{
//                devItem.isNeedUpgrade   =   YES;
//            }
            
            [tempArr addObject:devItem];
        }
        [rs close];
        arr = [tempArr copy];
    }];
    return arr;

}

@end
